var crypto = require('crypto');

function encrypt(key, iv, data) {
    var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
    var crypted = cipher.update(data, 'utf8', 'binary');
    crypted += cipher.final('binary');
    crypted = new Buffer(crypted, 'binary');
    return crypted;
}

/**
 * 解密方法
 * @param key      解密的key
 * @param iv       向量
 * @param crypted  密文
 * @returns string
 */
function decrypt(key, iv,crypted) {
    console.log("111111111111",crypted);
    crypted = new Buffer(crypted,'base64');
    console.log("2222222222222",crypted);
    let decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    let decoded = decipher.update(crypted, 'binary', 'utf8');
    try {
        decoded += decipher.final('utf8');
        return { code:200,decoded:decoded};
    }catch (e){
        return { code:500 };
    }
}

let getMd5 = function (key) {
    let Md5key = key;
    let md5=crypto.createHash("md5");
    md5.update(Md5key);
    let strMac = md5.digest('hex');
    return strMac.substring(0,16);
};

let key = getMd5('chongdingzhushou123');
let vi = getMd5('cdzs!@#');


var id = encrypt(key,vi,JSON.stringify({id:2874,  MacId:"WUD9r70svFUDANhWt6Fvo748jinyu",time:new Date().getTime(),}))
var token = encrypt(key,vi,JSON.stringify({id:2874}))
id = id.toString("base64")
token = token.toString("base64")
console.log("id is",id)


JsonString = decrypt(key, vi, "s4V4oUNiW++tYrxg/5jZRA==");
console.log("================",JsonString)

//
//
// var dataEn = encrypt(key,vi,JSON.stringify({
//     MacId:"WUD9r70svFUDANhWt6Fvo748",
//     time:new Date().getTime(),
//     sign:id,
//     token:token
// }))
// dataEn = dataEn.toString("base64")
// console.log("dataEn",dataEn)
//
//
// let info = chechSing(dataEn)
// console.log("info",info)
//
// function chechSing(signData) {
//     try {
//         let signDataString = decrypt(key, vi, signData);
//         console.log("0000000000000000",signDataString)
//         if (typeof signDataString === 'string') signDataString = JSON.parse(signDataString);
//         console.log("1111111",signDataString)
//
//         if (signDataString.code != 200)  return ({code: 500, msg: "加密错误1"});
//         const signDecodedInfo = JSON.parse(signDataString.decoded);
//
//         console.log("222222222222222",signDecodedInfo)
//
//         let sign = signDecodedInfo.sign;
//         let MacId = signDecodedInfo.MacId;
//
//         let sign_ = decrypt(key, vi, sign)
//         if (sign_.code != 200)  return ({code: 500, msg: "加密错误2"})
//         const sign_decoded = JSON.parse(sign_.decoded);
//
//         console.log("55555555",sign_decoded)
//
//         if (sign_decoded.MacId != MacId + "jinyu") return ({code: 500, msg: "macId 错误"})
//
//         if (parseInt(new Date().getTime()) - JSON.parse(sign_decoded.time) >= 1000 * 60 * 2) return ({
//             code: 500,
//             msg: "时间错误"
//         });
//         return {code: 200, signDecodedInfo: signDecodedInfo}
//     } catch (e) {
//         return ({code: 500, msg: "错误"+ e})
//     }
// }

/**
 * @time {string}  注册时间
 * @MacId {string}   机器码
 * @aliveTime {string} 激活时间 （月）
 */
exports.encrypt = encrypt;
exports.decrypt = decrypt;
exports.getMd5 = getMd5;
exports.key = key;
exports.vi = vi;




//
// [{"invalidKey":null,"ok":true,"data":{"clickUrl":"https://s.click.taobao.com/t?e=m%3D2%26s%3DwquhsqtEh5IcQipKwQzePOeEDrYVVa64LKpWJ%2Bin0XLjf2vlNIV67iOYn3JEXyyDJ7ATJSEv968wYyBkjhzGLZUSAhEZ2u8Q7TT703czVD3i4uK5eGhoPotmJ1gZa8j%2BXHDWGlmriXjyb3rAtnR9hyQ%2FGb7CwMvRDJbuZDCrHt4%3D&pvid=26_120.32.92.242_753_1537164092385","couponLink":"","tkCommonRate":"10.00","taoToken":"￥SgWYbVi6fyV￥","qrCodeUrl":"//gqrcode.alicdn.com/img?type=hv&text=https%3A%2F%2Fs.click.taobao.com%2Frpf8WMw%3Faf%3D3&h=300&w=300","type":"auction","couponShortLinkUrl":null,"shortLinkUrl":"https://s.click.taobao.com/rpf8WMw"},"info":{"ok":true,"message":null}},
//
//
//
//     {"invalidKey":null,"ok":true,"data":{"clickUrl":"https://s.click.taobao.com/t?e=m%3D2%26s%3DepDJuEKO7xwcQipKwQzePOeEDrYVVa64LKpWJ%2Bin0XLjf2vlNIV67vMVFNlHmuQK%2Fl0%2B1yuzCtIwYyBkjhzGLZUSAhEZ2u8Q7TT703czVD3i4uK5eGhoPotmJ1gZa8j%2Bh%2FR8Ft0GVcJvnfTa%2BwRDdPkfb5s4AhTnm8KuDTwPNQrGDF1NzTQoPw%3D%3D&pvid=26_120.32.92.242_657_1537164732069","couponLink":"https://uland.taobao.com/coupon/edetail?e=3bDs%2FANFS8YN%2BoQUE6FNzL5lHkcFpRe0L5pQlgNvPslF5m9PW%2FNgYJPa3WnEFCDC617002I4EVbImN7MFAbLs53RWFcZJqL72IaVVct%2FH6c%2FBw6SYxHp8hDwg5Iposw%2FLjtltp%2BmeYjmySRol2qpivV6VuFWweCR5vBI1YN2%2Fko%3D&af=1&pid=mm_187900144_104700467_20330200357","tkCommonRate":"30.00","taoToken":"￥rDE6bViQEjR￥","couponLinkTaoToken":"￥a8sfbViQsez￥","qrCodeUrl":"//gqrcode.alicdn.com/img?type=hv&text=https%3A%2F%2Fs.click.taobao.com%2FD267WMw%3Faf%3D3&h=300&w=300","type":"auction","couponShortLinkUrl":"https://s.click.taobao.com/NF87WMw","shortLinkUrl":"https://s.click.taobao.com/D267WMw"},"info":{"ok":true,"message":null}},
//
// ]

